package com.example.s07_restful_swagger.controller;

import com.example.s07_restful_swagger.pojo.BaseResult;
import com.example.s07_restful_swagger.pojo.Message;
import com.example.s07_restful_swagger.service.IMessageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value="消息",description = "消息api",position = 100,protocols = "http")
@RestController
@RequestMapping("/message/")
public class MessageController
{

    @Autowired
    private IMessageService messageService;

    // http://localhost:8080/message/findOne?id=1

    // http://localhost:8080/message/1
    //.http://localhost:8080/message/2

//    // 接口要提供增删改查
//    @GetMapping("{id}")
//    public Message findOne(@PathVariable("id") Long id)
//    {
//        Message message = messageService.findById(id);
//        return message;
//    }
//    @GetMapping("")
//    public List<Message> findAll()
//    {
//        List<Message> list = messageService.findAll();
//        return list;
//    }
//
//    @PostMapping("")
//    public String add(Message m)
//    {
//        messageService.save(m);
//        return "{result:\"添加成功\"}";
//    }
//
//
//    @DeleteMapping("{id}")
//    public String del(@PathVariable("id") Long id)
//    {
//        messageService.removeById(id);
//        return "{result:\"删除成功\"}";
//    }
//
//    @PutMapping("")
//    public String update(Message m)
//    {
//        messageService.updateById(m);
//        return "{result:\"修改成功\"}";
//    }




    @GetMapping("{id}")
    @ApiOperation(value = "查找消息", notes = "根据编号查找消息")
    public BaseResult<Message> findOne(@PathVariable("id") Long id)
    {
        Message message = messageService.findById(id);
        return BaseResult.success(message);
    }


    @ApiOperation(
            value = "消息列表",
            notes = "完整的消息内容列表",
            produces="application/json, application/xml",
            consumes="application/json, application/xml",
            response = List.class)
    @GetMapping("")
    public BaseResult<List<Message>> findAll()
    {
        List<Message> list = messageService.findAll();
        return BaseResult.success(list);
    }

    @ApiOperation(value = "添加消息", notes = "根据参数创建消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "text", value = "正文", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要", required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping("")
    public BaseResult add(@RequestBody Message m)
    {
        messageService.save(m);
        return BaseResult.success(null);
    }

    // post

    // 请求体    普通的表单提交
    // text=aaaa&summary=bbbb

    // 请求体中  js提交时有时会采用JSON方式               添加注解 @RequestBody
    // {
    //    text:"aaa",
    //    summary:"bbbb"
    // }


//    public BaseResult add(Message m)
//    {
//        messageService.save(m);
//        return BaseResult.success(null);
//    }

    @ApiOperation(value = "修改消息", notes = "根据参数修改消息")
    @ApiResponses({
            @ApiResponse(code = 100, message = "请求参数有误"),
            @ApiResponse(code = 101, message = "未授权"),
            @ApiResponse(code = 103, message = "禁止访问"),
            @ApiResponse(code = 104, message = "请求路径不存在"),
            @ApiResponse(code = 200, message = "服务器内部错误"),
            @ApiResponse(code = 1001, message = "没有找到,删除失败")
    })
    @DeleteMapping("{id}")
    public BaseResult del(@PathVariable("id") Long id)
    {
        boolean result = messageService.removeById(id);
        if(result) {
            return BaseResult.success(null);
        }
        else {
            return BaseResult.error(1001,"没有找到,删除失败!");
        }
    }

    @PutMapping("")
    public BaseResult update(Message m)
    {
        messageService.updateById(m);
        return BaseResult.success(null);
    }

}
